<?php
/**
 * $Id: user_favorites_controller.php 587 2010-07-05 05:14:47Z varun $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * @version 			$Rev: 587 $
 * @lastModifiedBy		$LastChangedBy: varun $
 * @lastChanged			$Date: 2010-07-05 10:44:47 +0530 (Mon, 05 Jul 2010) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

/**
 * Handles user favroite items based on $type and $item_id
 *
 */
class UserFavoritesController extends UsersAppController
{
	var $name = 'UserFavorites';
	var $components = array('Auth','RequestHandler');
	var $helpers = array('Users.UserContent');
	/**
	 * Adds favorite item.
	 * Needs permission before editing.
	 *
	 * @param unknown_type $item_id
	 * @param unknown_type $type
	 */
	function beforeFilter()
	{
		$this->Auth->allow('add','remove');
		parent::beforeFilter();
	}

	function add($itemIds=null, $type=null)
	{		
		Configure::write('debug',0);
		$message = '';
		$ajaxRequest = false;
		$this->layout = 'ajax';
		$itemAdded = false;
		$bulk = false;
		if(!$authedUser = $this->Session->read('Auth.User'))
		{
			$message = __("Please login before adding it to your favorites!");			
		}elseif($this->UserFavorite->find('count',array('conditions'=>array('UserFavorite.item_id'=>$itemIds,'type'=>$type,'user_id'=>$authedUser['id']))))
		{
			$message = sprintf(__('You have already added this %s to your favourites'),$type);
		}elseif($this->data)
		{
			$bulk = true;
			$params  = $this->_getBulkActionParams();
			$type = $params['model'];
			$itemIds = $params['ids'];
		}
		if(empty($message))
		{
			if(!is_array($itemIds))
				$itemIds = (array)$itemIds;
			array_filter($itemIds);
			foreach($itemIds as $item_id)
			{
				$newFav = array();
				$newFav['UserFavorite']['user_id'] = $authedUser['id'];
				$newFav['UserFavorite']['item_id'] = $item_id;
				$newFav['UserFavorite']['type'] = $type;
				$this->UserFavorite->create();
				$this->UserFavorite->save($newFav);
				$this->UserFavorite->id='';
				$itemAdded = true;
			}
			if($itemAdded)
			{
				callHooks('AfterFavouriteAdded','.+',$this,$newFav);
				//$message = sprintf(__("Selected %s added successfully."),count($itemIds)> 1 ? ' items were ' :' item was ');	
				$message = sprintf(__("%s has been added to your favorites"),$type);
			}
		}
		
		$this->setMessage($message);		
		if($this->RequestHandler->isAjax()==false)
		{
			$this->redirect($this->referer());
		}
		$itemId = $this->params['pass'][0];
		$this->set(compact('itemId','type','bulk'));
		//$this->render('blank');
	}
	
	/**
	 * Removed favorite item.
	 * Needs permission before editing.
	 *
	 * @param unknown_type $item_id
	 * @param unknown_type $type
	 */
	function remove($itemIds=null, $type=null)
	{
		Configure::write('debug','0');
		$this->layout = 'ajax';
		$bulk = false;
		$authedUser = $this->Session->read('Auth.User');
		if(empty($authedUser))
		{
			$this->setMessage(sprintf(__("Please login before removing %s from your favorites!"),$type));
		}else
		{
			if($this->data)
			{
				$params  = $this->_getBulkActionParams();
				$type = $params['model'];
				$itemIds = $params['ids'];
				$bulk = true;
			}
			$userFav = $this->UserFavorite->find('list',array('conditions'=>array('UserFavorite.item_id'=>$itemIds, 'UserFavorite.type'=>$type,'UserFavorite.user_id'=>$authedUser['id']),'fields'=>array('id')));
			$this->UserFavorite->deleteAll(array('id'=>$userFav));
			$message = sprintf(__('%s has been removed from your favorites.'),$type);
			$this->setMessage($message);
		}
		if($this->RequestHandler->isAjax()==false)
		{
			$this->redirect($this->referer());
		}
		$itemId = $this->params['pass'][0];
		$this->set(compact('itemId','type','bulk'));
		//$this->render('blank');
	}
	
	function setMessage($message)
	{
		$message = __($message);
		if($this->RequestHandler->isAjax()==true)
		{
			$this->set('message',$message);
		}else
		{
			$this->successMsg($message);
		}
	}



}
?>
